import sqlite3
import os

def count_records(db_path):
    """查询数据库中各表的记录数量"""
    try:
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        # 查询notice_table表的记录数
        cursor.execute("SELECT COUNT(*) FROM notice_table")
        notice_count = cursor.fetchone()[0]
        
        # 查询target_table表的记录数
        cursor.execute("SELECT COUNT(*) FROM target_table")
        target_count = cursor.fetchone()[0]
        
        # 查询goods_table表的记录数
        cursor.execute("SELECT COUNT(*) FROM goods_table")
        goods_count = cursor.fetchone()[0]
        
        conn.close()
        return (notice_count, target_count, goods_count)
    except Exception as e:
        print(f"查询{db_path}时出错: {e}")
        return (0, 0, 0)

def get_file_size(file_path):
    """获取文件大小（MB）"""
    try:
        size = os.path.getsize(file_path) / (1024 * 1024)
        return round(size, 2)
    except Exception as e:
        print(f"获取{file_path}大小出错: {e}")
        return 0

if __name__ == "__main__":
    print("比较jntujy.db和jntujy11.db数据库文件...")
    print("-" * 50)
    
    # 获取文件大小
    db1_size = get_file_size("jntujy.db")
    db2_size = get_file_size("jntujy11.db")
    
    print(f"jntujy.db 文件大小: {db1_size} MB")
    print(f"jntujy11.db 文件大小: {db2_size} MB")
    print(f"大小差异: {abs(db2_size - db1_size):.2f} MB ({(1 - min(db1_size, db2_size) / max(db1_size, db2_size)) * 100:.1f}%)")
    print("-" * 50)
    
    # 获取jntujy.db的记录数
    notice_count1, target_count1, goods_count1 = count_records("jntujy.db")
    total1 = notice_count1 + target_count1 + goods_count1
    
    print(f"jntujy.db 记录数：")
    print(f"  notice_table: {notice_count1}")
    print(f"  target_table: {target_count1}")
    print(f"  goods_table: {goods_count1}")
    print(f"  总记录数: {total1}")
    print("-" * 50)
    
    # 获取jntujy11.db的记录数
    notice_count2, target_count2, goods_count2 = count_records("jntujy11.db")
    total2 = notice_count2 + target_count2 + goods_count2
    
    print(f"jntujy11.db 记录数：")
    print(f"  notice_table: {notice_count2}")
    print(f"  target_table: {target_count2}")
    print(f"  goods_table: {goods_count2}")
    print(f"  总记录数: {total2}")
    print("-" * 50)
    
    # 比较数据量差异
    print(f"数据量比较：")
    print(f"  notice_table差异: {abs(notice_count2 - notice_count1)}")
    print(f"  target_table差异: {abs(target_count2 - target_count1)}")
    print(f"  goods_table差异: {abs(goods_count2 - goods_count1)}")
    print(f"  总记录数差异: {abs(total2 - total1)}")
    print(f"  差异百分比: {(abs(total2 - total1) / max(total1, total2)) * 100:.1f}%")